package com.zx.pay.wechat.controller;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.service.billdownload.BillDownloadService;
import com.wechat.pay.java.service.billdownload.model.GetFundFlowBillRequest;
import com.wechat.pay.java.service.billdownload.model.GetTradeBillRequest;
import com.wechat.pay.java.service.billdownload.model.QueryBillEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wechat/pay/bill")
public class BillController {

    @Autowired
    private Config config;

    /**
     * 申请交易账单
     */
    @PostMapping("/trade-bill")
    public QueryBillEntity applyTradeBill(
            @RequestParam String billDate,
            @RequestParam(required = false) String subMchid) {

        BillDownloadService service = new BillDownloadService.Builder().config(config).build();

        GetTradeBillRequest request = new GetTradeBillRequest();
        request.setBillDate(billDate);
        if (subMchid != null) {
            request.setSubMchid(subMchid);
        }

        return service.getTradeBill(request);
    }

    /**
     * 申请资金账单
     */
    @PostMapping("/fundflow-bill")
    public QueryBillEntity applyFundFlowBill(@RequestParam String billDate) {

        BillDownloadService service = new BillDownloadService.Builder().config(config).build();

        GetFundFlowBillRequest request = new GetFundFlowBillRequest();
        request.setBillDate(billDate);

        return service.getFundFlowBill(request);
    }
}